package pt.mauromad.guias.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pt.mauromad.guias.bean.UserBean;
import pt.mauromad.guias.controller.admin.AdminUserController;

@Controller
public class UserController extends AdminUserController {
	@PreAuthorize("hasAnyRole('ROLE_ACCOUNT_ADMIN')")
	@RequestMapping("/user/all")
	public String all(Model model) {
		return super.all(model, getDomain());
	}

	@RequestMapping(value = "/user", method = RequestMethod.GET)
	public String show(Model model) {
		return redirect("/user/%s", getUsername());
	}

	@RequestMapping(value = "/user/{username:[a-z0-9\\.]+}", method = RequestMethod.GET)
	public String show(Model model, @PathVariable String username) {
		return super.show(model, username, getDomain());
	}

	@RequestMapping(value = "/user/{username:[a-z0-9\\.]+}", method = RequestMethod.POST, params = "action=manage")
	public String manage(Model model, @PathVariable String username) {
		return super.manage(model, username, getDomain());
	}

	@RequestMapping(value = "/user/{username:[a-z0-9\\.]+}", method = RequestMethod.POST, params = "action=manage.save")
	public String manageSave(Model model, @Validated UserBean bean, @PathVariable String username) {
		super.manageSave(model, bean, username, getDomain());
		return redirect("/user/user/%s", username);
	}

	@PreAuthorize("hasAnyRole('ROLE_ACCOUNT_ADMIN')")
	@RequestMapping(value = "/user/new", method = RequestMethod.GET)
	public String createUser(Model model) {
		return super.newUser(model);
	}

	@PreAuthorize("hasAnyRole('ROLE_ACCOUNT_ADMIN')")
	@RequestMapping(value = "/user/new", method = RequestMethod.POST)
	public String saveNewUser(Model model, @Validated UserBean bean) {
		super.saveNewUser(model, bean, getDomain());
		return "/account";
	}
}
